package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.JifenChongzhiDAO;
import com.dao.UserInfoDAO;
import com.pojo.JifenChongzhi;
import com.pojo.UserInfo;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Auther: tz
 * @Date: 2023/11/29 16:26
 */
@Service
public class JifenChongzhiService extends ServiceImpl<JifenChongzhiDAO, JifenChongzhi> {

    @Autowired
    private JifenChongzhiDAO jifenChongzhiDAO;

    @Autowired
    private UserInfoDAO userInfoDAO;

    /**
     * 分页查询积分充值记录
     * @param page
     * @param limit
     * @param key
     * @param startTime
     * @param endTime
     * @return
     */
    public IPage getJifenChongzhiByPage(int page,int limit,String key,String startTime,String endTime){
        Page p = new Page(page,limit);
        QueryWrapper q = new QueryWrapper();
        if (StringUtil.isNotNull(startTime)){
            q.ge("czTime",startTime);
        }
        if (StringUtil.isNotNull(endTime)){
            q.le("czTime",endTime);
        }
        if (StringUtil.isNotNull(key)){
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
            queryWrapper.like("name","%"+key+"%");
            queryWrapper.or().like("phone","%"+key+"%");
            List<UserInfo> userInfoList = userInfoDAO.selectList(queryWrapper);
            if (userInfoList.size() > 0){
                String ids = "";
                for (UserInfo userInfo : userInfoList){
                    if (StringUtil.isNull(ids)){
                        ids = ids + userInfo.getId();
                    }else{
                        ids = ids + "," + userInfo.getId();
                    }
                }
                q.in("userId",ids);
            }
        }
        q.orderByDesc("czTime");
        IPage iPage = jifenChongzhiDAO.selectPage(p,q);
        return iPage;
    }

    /**
     * 新增充值记录
     * @param userId
     * @param jine
     * @param yue
     * @param openUser
     * @param openName
     * @param source
     * @return
     */
    public boolean addJifenChongzhi(String userId, BigDecimal jine, BigDecimal yue, String openUser, String openName, int source){
        JifenChongzhi jifenChongzhi = new JifenChongzhi();
        jifenChongzhi.setUserId(userId);
        jifenChongzhi.setJine(jine);
        jifenChongzhi.setYue(yue);
        jifenChongzhi.setCzTime(new Date());
        jifenChongzhi.setOperUser(openUser);
        jifenChongzhi.setOperName(openName);
        jifenChongzhi.setSource(source);
        if (jifenChongzhiDAO.insert(jifenChongzhi) == 1){
            return true;
        }
        return false;
    }
}
